<template>
	<view>

	</view>
</template>

<script>
	let that
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		name: "com-map",
		props:{
			
		},
		data() {
			return {
				
			};
		},
		mounted() {
			that = this
		},
		computed: {
			//引入store全局变量
			...mapState(['mylocation','mylocationState'])
		},
		methods: {
			//引入store全局方法
			...mapActions(['getlocationAction']),
			//获取当前定位
			getLocation() {
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						var myloc = {'latitude':res.latitude,'longitude':res.longitude}
						that.getlocationAction(myloc)//给store全局变量赋值
						that.$emit('myevent',myloc)//通过$emit给父组件传值，myevent和父组件的myevent要一样
					},
					fail: function(res) {
						console.log('定位失败')
					}
				})
			},
			//判断是否开启定位授权，如果未开启则引导开启手机定位权限
			setlocation() {
				uni.authorize({
					scope: 'scope.userLocation',
					success() { //1.1 允许授权
						console.log("scope.userLocation success")
						that.getLocation();
					},
					fail() { //1.2 拒绝授权
						uni.showModal({
							title:'是否授权当前位置',
							content: '需要获取您的地理位置，请确认授权，否则部分功能将无法使用',
							confirmText: "确认",
							cancelText: '取消',
							success: (res) => {
								if (res.confirm) {
									uni.openSetting({
										success: (res) => {
											that.getLocation();
										}
									})
								} else {
									that.$u.toast('您已取消授权')
									return false;
								}
							}
						})
						return false;
					}
				})
			},
			//导航到第三方APP
			toMapAPP(address,longitude,latitude){
				const result =  this.$util.wgs84ToGcj102(longitude,latitude);
				uni.openLocation({
					latitude: result[1],
					longitude: result[0],
					address:address,
					success: function () {
						console.log('openLocation success');
					},
					fail:function(e){
						console.log("openLocation fail",e);
					}
				});
			}
		}
	}
</script>

<style>

</style>